<template>
    <div class="order-report">
        <div class="report-table-form1">
            <div class="report-table-form1-line">
                <div
                    class="report-table-form1-item word samll"
                    style="width: 50px"
                >
                    代理商:
                </div>
                <div class="report-table-form1-item" style="width: 180px">
                    <el-select
                        v-model="agentName"
                        filterable
                        placeholder="请选择"
                        @change="getServiceList"
                        size="small"
                    >
                        <el-option
                            v-for="item in agentList"
                            :key="item.agentName"
                            :label="item.agentName"
                            :value="item.agentName"
                        ></el-option>
                    </el-select>
                </div>
            </div>
        </div>
        <div class="report-table-wrapper">
            <el-table
                :header-cell-style="headerCellStyle"
                size="small"
                :data="serviceList"
                border
                style="width: 100%"
            >
                <el-table-column
                    prop="addServiceCode"
                    label="应用编码"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="addServiceName"
                    label="应用名称"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="agentPrice"
                    label="代理商成本价"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="originalPrice"
                    label="成本价"
                    min-width="100"
                ></el-table-column>
                <el-table-column
                    prop="description"
                    label="应用描述"
                    min-width="100"
                ></el-table-column>
                <el-table-column label="操作" min-width="130">
                    <template slot-scope="scope">
                        <div style="margin-bottom: 10px">
                            <el-button
                                style="margin-right: 10px"
                                size="mini"
                                type="primary"
                                @click="toModify(scope.row)"
                                >修改</el-button
                            >
                        </div>
                    </template>
                </el-table-column>
                <!--  <el-table-column type="expand">
                    <template slot-scope="props">
                        <div style="line-height: 18px;font-size: 16px">
                        </div>
                    </template>
                </el-table-column> -->
            </el-table>
        </div>

        <div class="report-table-block" v-show="totalNum > pageSize">
            <div class="report-table-block-in">
                <el-pagination
                    background
                    :current-page.sync="currentPage"
                    @current-change="handleCurrentChange"
                    :page-size="pageSize"
                    layout="prev, pager, next, total"
                    :total="totalNum"
                ></el-pagination>
            </div>
        </div>
        <!-- 弹窗 -->
        <el-dialog
            title="修改信息"
            :visible.sync="dialogShowEdit"
            width="500px"
        >
            <div class="line">
                <div class="lable">代理商成本价：</div>
                <div class="input">
                    <el-input
                        v-model="service.agentPrice"
                        placeholder="必填"
                        size="small"
                        @blur="agentPriceChage"
                    ></el-input>
                </div>
            </div>
            <div class="line" style="text-align: center; padding: 10px 0 30px">
                <el-button type="primary" size="small" plain @click="submit"
                    >确认修改</el-button
                >
            </div>
        </el-dialog>
    </div>
</template>

<script type="text/ecmascript-6">
export default {
    data: function () {
        return {
            agentName: '',
            agentList: [],
            adminId: '',
            currentPage: 1,
            pageSize: 10,
            totalNum: 0,
            serverIp: '',
            serviceList: [],
            dialogShowEdit: false,
            service: {},
            statusList: [
                {
                    value: 1,
                    statusName: '正常'
                },
                {
                    value: 0,
                    statusName: '停用'
                }
            ]
        }
    },
    mounted () {
    },
    created () {
        // let lastIndex = this.ip.lastIndexOf('/')
        // this.$store.state.serverIp = this.ip.slice(0, lastIndex)
        this.getAgentList()
    },
    watch: {
        // service: {
        //     deep: true,
        //     handler (newVal, oldVal) {
        //         console.log('cheng')
        //         if (!this.service.id) {
        //             return
        //         }
        //         this.service.originalPrice = this.fix2(this.service.originalPrice)
        //         this.service.marketPrice = this.fix2(this.service.marketPrice)
        //         this.service.agentPrice = this.fix2(this.service.agentPrice)
        //         this.$forceUpdate()
        //     }
        // }
    },
    components: {
    },
    props: {
        underLine: null
    },
    computed: {
        companyId () {
            // return 660169
            return this.$store.state.page1KTVAgentEdit
        },
        ip () {
            return this.$store.state.IP
        }
    },
    methods: {
        async getAgentList () {
            const res = await this.$http.post(this.$store.state.serverIp + '/server/order/recon/agent/list', {
                token: localStorage.AuthorizationToken2
            })
            let response = res.data
            if (response.ret === '0') {
                this.agentList = response.data || []
                if (this.agentList.length > 0) {
                    this.agentName = this.agentList[0].agentName
                    this.getServiceList()
                }
            }
        },
        focus (event) {
            event.currentTarget.select()
        },
        fix2 (num) {
            // 保留两位小数
            return Math.round(num * 100) / 100
        },
        originalPriceChage (value) {
            this.$set(this.service, 'originalPrice', this.fix2(this.service.originalPrice))
            this.$forceUpdate()
        },
        marketPriceChage () {
            this.$set(this.service, 'marketPrice', this.fix2(this.service.marketPrice))
            this.$forceUpdate()
        },
        agentPriceChage () {
            this.$set(this.service, 'agentPrice', this.fix2(this.service.agentPrice))
            this.$forceUpdate()
        },
        handleCurrentChange () {
            // 换页
            this.currentPage = 1
        },
        reSearch () {
            // 点击搜索
            this.currentPage = 1
            this.getServiceList()
        },
        getServiceList () {
            // 获取 所有 服务列表
            this.$http.post(this.$store.state.serverIp + '/server/order/recon/serverinfo/list', {
                agentName: this.agentName,
                token: localStorage.AuthorizationToken2
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                response = response.body
                this.serviceList = []
                if (response.ret === '0') {
                    this.serviceList = response.data
                }
                if (response.ret !== '0') {
                    console.log('调用获取所有代理商失败')
                }
                console.log('******************************************')
            }, response => {
                console.log('通讯失败')
            })
        },
        toModify (service) {
            // 编辑 服务
            // Object.keys(service).map(key => {
            //     this.$set(this.service, key, service[key])
            // })
            console.log(service)
            this.service = this.copyArr(service)
            this.dialogShowEdit = true
        },
        submit () {
            if (this.service.agentPrice === '') {
                this.$message({
                    showClose: true,
                    message: '价格不能为空',
                    type: 'warning'
                })
                return
            }
            this.$http.post(this.$store.state.serverIp + '/server/order/recon/agent/serverinfo/edit', {
                agentName: this.agentName,
                token: localStorage.AuthorizationToken2,
                addServiceCode: this.service.addServiceCode,
                originalPrice: this.service.agentPrice
            },
                {
                    'emulateJSON': false
                }
            ).then(response => {
                response = response.body
                if (response.ret === '0') {
                    this.$message({
                        showClose: true,
                        message: '修改成功',
                        type: 'success'
                    })
                    this.getServiceList()
                    this.dialogShowEdit = false
                }
                if (response.ret !== '0') {
                    console.log('调用获取所有代理商失败')
                }
                console.log('******************************************')
            }, response => {
                console.log('通讯失败')
            })
        }
    }
}
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
.order-report
    width: 100%
    background: #fff
    min-height: calc(100vh - 65px)
    position: relative

    .report-table-top-title
        width: 100%
        height: 64px
        line-height: 64px
        color: #000
        font-size: 18px
        background: #f1f0f5

    .report-table-form1
        width: 100%
        min-height: 40px
        padding: 0px 0 0 22px
        box-sizing: border-box
        background: #fff
        overflow: hidden

        .report-table-form1-line
            min-height: 32px
            margin-bottom: 16px

            .report-table-form1-item
                display: inline-block
                vertical-align: top
                height: 32px
                margin-right: 15px
                line-height: 32px

                &.word
                    margin-right: 0

                &.samll
                    font-size: 15px
                    text-align: left

                &.big
                    margin-right: 100px

    .report-table-block
        width: 100%
        padding-top: 22px
        height: 50px
        background: #fff
        box-sizing: border-box

        .report-table-block-in
            float: right
            padding-right: 17px

    .page-padding-gray-small
        width: 100%
        height: 22px
        background: #f1f0f5

    .page-padding
        width: 100%
        height: 20px
        background: #fff

    .report-table-total
        width: 100%
        font-size: 16px
        color: #409EFF
        padding: 22px
        margin-top: 22px
        box-sizing: border-box
        background: #fff

    .page-all-loading
        position: fixed
        top: 55%
        left: 40%
        width: 200px

.line
    margin-bottom: 15px
    line-height: 35px

.lable
    display: inline-block
    width: 100px
    height: 30px
    font-size: 14px
    text-align: right
    padding-right: 14px
    vertical-align: top

.sub_title
    display: inline-block
    font-size: 30px

.input
    display: inline-block
    width: 200px

    &.width350
        width: 350px

.fileBg
    border: 1px solid #ddd
    border-radius: 6px
    width: 220px
    height: 140px

    img
        width: 100%
        height: 100%

.avatar-uploader
    position: absolute
    width: 220px
    height: 140px
    opacity: 0
    cursor: pointer

.avatar-uploader .el-upload
    border: 1px dashed #d9d9d9
    border-radius: 6px
    cursor: pointer
    position: relative
    overflow: hidden

.avatar-uploader .el-upload:hover
    border-color: #409EFF

.avatar-uploader-icon
    font-size: 28px
    color: #8c939d
    width: 220px
    height: 140px
    line-height: 140px
    text-align: center
</style>
